package com.fs.dataservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.entity.DataQuery;
import com.fs.common.utils.AuthUtils;
import com.fs.common.utils.HttpRequest;
import com.fs.dataservice.service.IDataService;
import org.springframework.stereotype.Service;
import org.xidea.el.json.JSONDecoder;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.Map;
import java.util.logging.Logger;

import static com.fs.common.utils.AuthUtils.RasAndDesDecode;
/**
 * @Author: wdf
 * @Description:
 * @CreateDate: 2019/8/19 0019$
 */
@Service
public class DataServiceImpl implements IDataService {
    final Logger log = Logger.getLogger(DataServiceImpl.class.getName());
    @Override
    public Map  getDate(DataQuery query) throws Exception {
        //加密过程
        String uid = "819cfb1850ce48e1ab954aed468b2089";
        String serviceUrl = "http://api.globaldaas.com/service";
        String serverName = "getEntByKeyword"; //服务方法名称
        String serverParams = AuthUtils.getServerJsonStr(uid, serverName,query.getKeyWord());
        log.info("请求参数"+"-------------"+serverParams);
        String postParams = MessageFormat.format("uid={0}&data={1}", uid, AuthUtils.getPostJsonStr(serverParams));
        //获取数据   String postParams = MessageF
        String result = HttpRequest.sendPost(serviceUrl, postParams);
        result = URLDecoder.decode(result, "UTF-8");
        //转换数据
        Map<String, Object> resultMap = JSONDecoder.decode(result);
        //解密数据
        try {
            String value = RasAndDesDecode(resultMap);
            log.info("返回结果"+"-------------"+value);
            Map json = (Map) JSONObject.parse(value);
            Map resultdata = (Map)json.get("RESULTDATA");
            Object errorcode = resultdata.get("ERRORCODE");
            if (errorcode != null){
                return null;
            }else{
                return resultdata;
            }
        } catch (Exception ex) {

        }

        return null;
    }
}
